﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.School;
using RegisterationCommon.Common;
using System.Data;

namespace RegisterationSystem.Space_School
{
    public partial class accounts_manage : System.Web.UI.Page
    {
        public string Sid;
        public string SchoolSuperAccount;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Sid =Cookies.Get("ID").ToString();
                SchoolSuperAccount = Cookies.Get("SchoolSuperAccount").ToString();
                switch (Request.Form["action"])
                {
                    case "del":
                        DelSchoolacc();
                        break;
                    case "upd":
                        UpdSchoolacc();
                        break;
                    case "delacc":
                        DelSchoolaccByID();
                        break;

                }
            }
        }
        // 根据ID删除学校帐号
        void DelSchoolaccByID()
        {
            string id = Request.Form["id"];
            SchoolAccount s = new SchoolAccount();
            s.ID = Convert.ToInt32(id);
            s.DelSchoolAccount();
        }
        //修改帐号状态
        void UpdSchoolacc()
        {
            string id = Request.Form["id"];
            string status = Request.Form["status"];
            SchoolAccount s = new SchoolAccount();
            s.ID = Convert.ToInt32(id);
            s.IsDisable = Convert.ToBoolean(Convert.ToInt32(status));
            s.MdfSchoolAccountStatus();
        }
        //删除学校帐号
        void DelSchoolacc()
        {
            //获取删除学校帐号集合
            string idlist = Request.Form["id"];
            idlist = idlist.Substring(0, idlist.Length - 1);
            DBAction.Delete("SchoolAccount", "id in (" + idlist + ")");

        }
        //添加
        protected void btnadd_Click(object sender, EventArgs e)
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from SchoolAccount where AccountName='" + txtname.Value.Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                Message.Send(10024);
                return;
            }

            RParams RP = new RParams();
            RP.Add("AccountName", txtname.Value.Trim(), SqlDbType.VarChar);
            RP.Add("AccountPswd", BaseLibrary.ConvertToMD5(txtpwd.Value.Trim().ToLower()), SqlDbType.VarChar);
            RP.Add("IsSuper", ddlstatus.Value == "0" ? false : true, SqlDbType.Bit);
            RP.Add("IsDisable", false, SqlDbType.Bit);
            RP.Add("SchID", Convert.ToInt32(Cookies.Get("SchID")), SqlDbType.Int);
            RP.Add("SuperAccount", 0, SqlDbType.Int);
            if (SchoolAccount.Current.AddSchoolAccount(RP))
            {
                Message.Send(10022);
            }
            else
            {
                Message.Send(10023);
            }
        }
    }
}